/**
 * 
 */
package com.sad.service;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sad.util.G;

/**
 * @author zhaoqy
 *
 */
@SuppressWarnings({ "rawtypes" })
public class ServiceManager {
	private static Logger logger = LoggerFactory.getLogger(ServiceManager.class);
	private static final Map<String, IService> SERVICE = new HashMap<String, IService>();

	/**
	 * 
	 * @param name
	 * @return
	 */
	public static IService allo(String name) {
		String className = G.system.service_package + "." + StringUtils.capitalize(name)
				+ "Service";
		IService p = SERVICE.get(className);
		if (p != null) {

			if (logger.isDebugEnabled()) {
				logger.debug("get service from cache : ", className);
			}

			return p;
		}

		try {
			p = (IService) Class.forName(className).newInstance();
			SERVICE.put(className, p);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}

		return p;
	}
}
